package Stream;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Stream方法
 */
public class StreamMethod {
    //提取集合中所有偶数并求和
    @Test
    public void method1() {
        List<String> list = Arrays.asList("1", "2", "3", "4", "5");
        int sum = list.stream()//获取对象
                .mapToInt(s -> Integer.parseInt(s))//转化为整数
                .filter(n -> n % 2 == 0)//过滤
                .sum();
        System.out.println(sum);
    }

    //所有名字首字母大写
    @Test
    public void method2() {
        List<String> list = Arrays.asList("java", "web", "mysql", "python");
        List newlist = list.stream()
                .map(s -> s.substring(0, 1).toUpperCase() + s.substring(1))
//                .forEach(s -> System.out.println(s));
                .collect(Collectors.toList());
        System.out.println(newlist);

    }

    //所有奇数从大到小排序且不重复
    @Test
    public void method3() {
        List<Integer> list = Arrays.asList(456,8,2,967,12,3,45,6,789,46,6,4,7);
        List newlist = list.stream().distinct()//去除重复数据
                .filter(s -> s % 2 == 1)
                .sorted((a,b)-> b-a)
                .collect(Collectors.toList());
        System.out.println(newlist);

    }

}
